package base.utils;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class AccessStatisticsIntceptor implements HandlerInterceptor {

	public static long access_num = 0;
	public static Map<String,Long> IPMap = new HashMap<String,Long>();
	public static Map<String,Long> urlMap = new HashMap<String,Long>();

	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//		String url = request.getServletPath();
		//不过滤的
		if(new PathAsURL(request).isPass())return true;
		//要过滤，能通过
		HttpSession session = request.getSession();
		String controlURI = new PathAsURL(request).getControlURI();
		String URL = request.getContextPath();
		String projectName = request.getContextPath();
		
		if (session.getAttribute("user") == null 
				&& (controlURI.equals("/user") || controlURI.equals("/carts") || controlURI.equals("/coll") )){
			URL =projectName + "/user" + "/login.do";
		}else if((session.getAttribute("user") == null && session.getAttribute("sup") == null) && controlURI.equals("/orders")){
			URL =projectName + "/user" + "/login.do";
		}else if(session.getAttribute("sup") == null && session.getAttribute("admin") == null && (controlURI.equals("/sup") )){
			URL =projectName + "/sup" + "/login.do";
		}else if(session.getAttribute("admin") == null && controlURI.equals("/admin")){
			URL =projectName + "/admin" + "/login.do";
		}else if(session.getAttribute("user") != null && session.getAttribute("sup") == null && controlURI.equals("/sup")){
			URL =projectName + "/sup" + "/login.do";
		}else if(session.getAttribute("user") != null && session.getAttribute("admin") == null && controlURI.equals("/admin")){
			URL =projectName + "/admin" + "/login.do";
		}else if(session.getAttribute("sup") != null && session.getAttribute("user") == null
				&& (controlURI.equals("/user") || controlURI.equals("/carts") || controlURI.equals("/coll"))){
			URL =projectName + "/user" + "/login.do";
		}else if(session.getAttribute("sup") != null && session.getAttribute("admin") == null && controlURI.equals("/admin")){
			URL =projectName + "/admin" + "/login.do";
		}/*else if(session.getAttribute("admin") != null && session.getAttribute("sup") == null && controlURI.equals("/sup")){
			URL =projectName + "/sup" + "/login.do";
		}*/else if(session.getAttribute("user") != null || session.getAttribute("sup") != null || session.getAttribute("admin") != null){
			return true;
		}
		
		
		
//		if(controlURI.equals("/user") || controlURI.equals("/admin") || controlURI.equals("/sup") ){
//			URL += controlURI + "/login.do";
//		}else if(controlURI.equals("/carts") || controlURI.equals("/orders") || controlURI.equals("/coll")){
//			URL += "/user" + "/login.do";
//		}
		//过滤失败，跳转
		response.sendRedirect(URL);
		
		//将要跳转的地址放入session
		String URI = request.getRequestURI();
		String parameter = request.getQueryString();
		if(request.getParameter("json") != null){
			parameter = null;
		}
		if (parameter==null||parameter.equals("")){
			String totalURI = URI;
			session.setAttribute("URI", totalURI);
		}else{
			String totalURI = URI +"?"+ parameter;
			session.setAttribute("URI", totalURI);
		}
		
	    return false;
	}

	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
			Object arg2, ModelAndView arg3) throws Exception {
	}

	public void afterCompletion(HttpServletRequest arg0,
			HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
	}
}